// $Id: reactor.h 40 2011-04-15 15:44:23Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2010 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef REACTOR_H_
#define REACTOR_H_

#include <pthread.h>

#include "nio.h"
#include "future.h"
#include "radiator.h"

#ifdef WIN32
#include <fcntl.h>
#define pipe(fds) _pipe(fds,4096,_O_BINARY)
#endif

struct event_base;

#define PIPERECV 0
#define PIPESEND 1

struct nioreactor {
	struct event_base *evbase;
	struct niofutures futures;
	int breaking;
	int pipe[2];
	struct event event;
	struct nioradiator *radiator;
	pthread_mutex_t lock;
};

int nioreactor_insert(struct nioreactor *reactor, struct niofuture *future,
		struct niosocket *socket, enum niofuture_flag flag,
		struct timeval *timeout, niofuture_cb cb, void *cbarg);

int nioreactor_remove(struct nioreactor *reactor, struct niofuture *future);

int nioreactor_wake_up(struct nioreactor *reactor);

inline int nioreactor_awaken_do(struct nioreactor *reactor);

void nioreactor_awaken_cb(int fd, short event, void *arg);

void nioreactor_event_cb(int fd, short event, void *arg);

int nioreactor_activate(struct nioreactor *reactor, struct niofuture *future);

inline int nioreactor_dispatch(struct nioreactor *reactor,
		struct niofuture *future);

#endif /* REACTOR_H_ */
